<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/7/18
 * Time: 下午4:00
 */

namespace app\index\service;
use app\index\model\Level;
use app\index\model\Quota;
use app\index\model\SubCategory;

class StandardService
{
    /**
     * @param $cateid
     * 通过二级资质id 获取对应的资质办理标准详情,如果该资质含有等级,把所有等级的数据都返回
     */
    const STANDARD_DETAIL_KEY = "STANDARD_DETAIL_KEY";
    const EXPIRE_IN = 60*60*12;
    public static  function getStandardDetailById($supcateId,$subcateId){
        $cache_standard_detail = cache(self::STANDARD_DETAIL_KEY.$supcateId.$subcateId);
        if($cache_standard_detail){
            return $cache_standard_detail;
        }

        $data = Quota::getQuotaListById($supcateId,$subcateId);


        $standrad = [];
        $scope = [];
        $title = SubCategory::getCategoryNameById($subcateId);
        $levels = $title."专业资质分为";
        $ids = [];
        foreach ($data as $item){
            $tmp_level = Level::getLevelNameById($item["level"]);
            $standrad_item = [];
            $scope_item = [];
            $ids[] = $item['id'];
            $levels.=$tmp_level;
            $standrad_item['sub_title']= $tmp_level."办理标准";
            $standrad_item['content'] = $item['standrad'];
            $scope_item['sub_title']= $tmp_level."办理范围";
            $scope_item['content'] = $item['scope'];
            array_push($standrad,$standrad_item);
            array_push($scope,$scope_item);
        }

        sort($ids);
        if(count($ids) == 1){

            $last_id = Quota::getLastId($ids[0]);

            $next_id = Quota::getNextId($ids[0]);
        }else{

            $last_id = Quota::getLastId(array_shift($ids));
            $next_id = Quota::getNextId(array_pop($ids));
        }



        $last_news_model = Quota::getOne($last_id);
        $last_news["supcategory_id"] = $last_news_model['supcategory_id'];
        $last_news["subcategory_id"] = $last_news_model['subcategory_id'];
        $last_news['title'] = SubCategory::getCategoryNameById($last_news_model['subcategory_id']);

        $next_news_model = Quota::getOne($next_id);
        $next_news["supcategory_id"] = $next_news_model['supcategory_id'];
        $next_news["subcategory_id"] = $next_news_model['subcategory_id'];
        $next_news['title'] = SubCategory::getCategoryNameById($next_news_model['subcategory_id']);
        if($last_news_model ==null){
            $last_news["supcategory_id"] = 0;
            $last_news["subcategory_id"] = 0;
            $last_news["title"] = "没有上一条了";

        }
        if($next_news_model ==null){
            $next_news["supcategory_id"] = 0;
            $next_news["subcategory_id"] = 0;
            $next_news["title"] = "没有下一条了";

        }
        $recommend = Quota::getRecList();
        $ret = [
            'title'=>$title,
            'key_words'=>$title,
            'desc'=>$levels,
            'standrad'=>$standrad,
            'scope'=>$scope,
            'last_news'=>$last_news,
            'next_news'=>$next_news,
            'recommend'=>$recommend

        ];
        cache(self::STANDARD_DETAIL_KEY.$supcateId.$subcateId, $ret, self::EXPIRE_IN);
        return $ret;
    }
}